package com.yb.personelmanagement.service.login;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.yb.personelmanagement.domain.System.Result;
import com.yb.personelmanagement.domain.System.User;
import com.yb.personelmanagement.domain.System.UserRepository;

@Service
public class LoginServiceImpl implements LoginService {

	@Autowired
	UserRepository userRepository;
	@Autowired
	PasswordEncoder passwordEncoder;
	
	@Override
	public Result checkUser(String userName, String passWord) {
		
		
		if(StringUtils.isEmpty(userName)) {
			
			return new Result(false,null,"用户名不能为空",null);
			
		}else {
			
			if(StringUtils.isEmpty(passWord)) {
				return new Result(false,null,"用户名不能为空",null);
			}
			else {
				User user=userRepository.findByUserName(userName);
				if(null!=user && passwordEncoder.matches(passWord, user.getPassword())) {
					
				return new Result(true,user,"登陆成功",null);
					
				}
				else {
					return new Result(false,null,"用户名或密码不正确",null);
				}
			}
		}
		
	}

}
